var historJs = 'js/pcsuccess/main.js';

var historTpl = 'html/pcsuccess/main.tpl';

var params = null;

$(function(e){
	pcIndex_dataInit();
});

function pcIndex_dataInit(){
	pcIndex_eventInit();
	
}

function pcIndex_eventInit(){
	// AjaxGetUtil
	AjaxGetUtil.request({url:path+"06cfeed0612242989dc2f544fa93838d",params:{},type:'json',callback:function(json){
		if(json.returnCode==0){
			console.log(json);
			$("#userName").html(json.bean.loginName);
			//使用Handlerbars的helper代码块进行分类编译
			Handlebars.registerHelper("compare1", function(v1, v2, options){
				if(v1 == null || v1 == ""){
					return options.fn(this);
				} else {
					return options.inverse(this);
				}
			});
			var source = $("#select_menu").html();
			var template = Handlebars.compile(source);
			$("#menuDiv").html(template(json));
		    
			//获取cookie中加载的tpl代码块和js
		    var tpl = $.cookie('historTPL');
			var tpl_js = $.cookie('historJS');
			
			if(!isNull($.cookie('historParams'))){
				params = JSON.parse($.cookie('historParams'));
			}
			if(!isNull(tpl) && !isNull(tpl_js)){
				historJs = tpl_js;
				historTpl = tpl;
			}
			//对reftpl进行匹配，然后选择他的父级位置
			$("a[reftpl='"+historTpl+"']").parent().parent().css("display","block");
			if($("a[reftpl='"+historTpl+"']").attr("ty")==2){
				$("a[reftpl='"+historTpl+"']").parent().parent().parent().addClass("open");
			}
			$.ajaxSetup ({ cache: false }); 
			
			//将获取到tpl代码块和js放到相应的区域中，在pcsuccess.html中
			$("#page-content").load(path + historTpl, null, function(response, status, xhr) {
				$('#page-content').html(response);
				$.getScript(path + historJs,function(){});
				$.cookie('historTPL', historTpl, { path: '/' });
				$.cookie('historJS', historJs, { path: '/' });
				$.cookie('historParams', JSON.stringify(params), { path: '/' });
			});
			
		}else{
			$.cookie('historTPL', null, { path: '/' });
			$.cookie('historJS', null, { path: '/' });
			$.cookie('historParams', null, { path: '/' });
			
			
			
			qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			window.setTimeout("window.location='"+path+"html/login/loginJudge.jsp'",3000); 
		}
	}});
	
	
	//菜单点击的动态效果
	$('body').on('click', '.itemHasChildOpen', function(e){
		if(!$(this).parent().hasClass("open")){
			$(this).parent().addClass("open");
			$(this).parent().find("ul").css("display","block");
			
		}else{
			$(this).parent().removeClass("open");
			$(this).parent().find("ul").css("display","none");
			
		}
	});
	
	//某一菜单的点击事件，刷新后加载数据
	$('body').on('click', '.itemHasChild', function(e){
		historTpl = $(this).attr("refTpl");
		historJs = $(this).attr("refJs");
		params = null;
		$.getScript(path + historJs,function(){});
		$.cookie('historTPL', historTpl, { path: '/' });
		$.cookie('historJS', historJs, { path: '/' });
		$.cookie('historParams', JSON.stringify(params), { path: '/' });
		window.location.reload();
	});
	
	//退出事件
	$('body').on('click', '#signOut', function(e){
		$.cookie('historTPL', null, { path: '/' });
		$.cookie('historJS', null, { path: '/' });
		$.cookie('historParams', null, { path: '/' });
		location.href = path + 'html/login/loginJudge.jsp';
	});
	
}

//跳转时，将加载到的数据赋值给相应的代码块区域中
function _createTab(_tpl, _js, _param){
	if(historTpl==_tpl && historJs == _js){
	}else{
		$("script[src='" + path + historJs + "']").remove();
		historTpl = _tpl;
		historJs = _js;
		params = eval(_param);
		$("#page-content").load(path + historTpl, null, function(response, status, xhr) {
			$('#page-content').html(response);
			$.getScript(path + historJs,function(){});
			$.cookie('historTPL', historTpl, { path: '/' });
			$.cookie('historJS', historJs, { path: '/' });
			$.cookie('historParams', JSON.stringify(params), { path: '/' });
		});
		window.location.reload();
	}
}
